package guo.mytesttreeview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.Toast;

import com.chen.treeview.TreeRecyclerView;
import com.chen.treeview.listener.OnNodeItemClickListener;
import com.chen.treeview.model.Node;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import guo.mytesttreeview.bean.DataBean;
import guo.mytesttreeview.bean.LocalNode;
import guo.mytesttreeview.bean.PowerModel;
import guo.mytesttreeview.bean.ServerNode;
import guo.mytesttreeview.utils.GetJsonDataUtil;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TreeRecyclerView tree_view;
    private List<DataBean> beanList = new ArrayList<>();

    HashMap<Integer, List<LocalNode>> map = new HashMap<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tree_view = (TreeRecyclerView) findViewById(R.id.tree_view);
        //传入数据，以及对应的模式
        tree_view.setData(getTestData(), TreeRecyclerView.MODE_CLICK_SELECT);
        tree_view.setOnItemClickListener(new OnNodeItemClickListener() {
            @Override
            public void onItemClick(Object o) {
                PowerModel powerModel = (PowerModel) o;
                Toast.makeText(MainActivity.this,powerModel.getName()+" --- "+powerModel.getId(),Toast.LENGTH_SHORT).show();
            }
        });

        test();
    }

    private List<PowerModel> getTestData(){
        ArrayList<PowerModel> result = new ArrayList<>();

        PowerModel powerModel = new PowerModel();
        powerModel.id = "190";
        powerModel.name = "界港分布式";
        powerModel.parentId = "1";
        powerModel.child = null;


        PowerModel powerModel2 = new PowerModel();
        powerModel2.id = "145";
        powerModel2.name = "江国庆";
        powerModel2.parentId = "190";
        powerModel2.child = null;


        PowerModel powerModel3 = new PowerModel();
        powerModel3.id = "197";
        powerModel3.name = "阳光伟1";
        powerModel3.parentId = "145";
        powerModel3.child = null;

        PowerModel powerModel4 = new PowerModel();
        powerModel4.id = "198";
        powerModel4.name = "江曹辉1";
        powerModel4.parentId = "190";
        powerModel4.child = null;


        PowerModel powerModel5 = new PowerModel();
        powerModel5.id = "372";
        powerModel5.name = "江昌云";
        powerModel5.parentId = "190";
        powerModel5.child = null;


        ArrayList<PowerModel> list_41 = new ArrayList<>();
        list_41.add(powerModel3);
        powerModel2.child = list_41;

        ArrayList<PowerModel> list_31 = new ArrayList<>();
        list_31.add(powerModel5);
        list_31.add(powerModel4);
        list_31.add(powerModel2);
        powerModel.child = list_31;

        result.add(powerModel);

        Log.d(TAG, result.toString());

        return result;

    }

    private void putValue(int key, ServerNode serverNode) {
        LocalNode localNode = new LocalNode();
        localNode.id = serverNode.getPowerId()+"";
        localNode.parentId = serverNode.getParentId()+"";
        localNode.name = serverNode.getPowerName();

        if (map.containsKey(key)) {
            map.get(key).add(localNode);
        } else {
            List<LocalNode> list = new ArrayList<>();
            list.add(localNode);
            map.put(key,list);
        }
    }


    private void generateList(List<LocalNode> list, int parentId) {

        list = map.get(parentId);

        if (list != null) {
            map.remove(parentId);
        }

        if (map.size() == 0) {
            Log.d(TAG, "list1111---->" + list);
            return;
        } else {
            for (LocalNode node : list) {
                int id = Integer.parseInt(node.id);
                generateList(node.child, id);
            }
            Log.d(TAG, "list3333---->" + list);
        }
        Log.d(TAG, "list2222---->" + list);
    }

    public void test() {

        List<ServerNode> serverList = getData();
        Log.d(TAG, "serverList------------>" + serverList);
        List<LocalNode> localList = new ArrayList<>();

        for(ServerNode node:serverList) {
            //生成map
            putValue(node.parentId,node);
        }
        Log.d(TAG, "map------------>" + map.toString());
        generateList(localList,1);

        Log.d(TAG, "localList------------->"+localList.toString());
    }




    private List<ServerNode> getData(){
        List<ServerNode> beanList = new ArrayList<>();
        String jsondata = new GetJsonDataUtil().getJson(this,"power.json");
        try {
            JSONArray data = new JSONArray(jsondata);
            for(int i=0;i<data.length();i++){
                ServerNode bean = new ServerNode();
                JSONObject jsonObject1 = data.getJSONObject(i);
                bean.setParentId(jsonObject1.getInt("parentId"));
                bean.setPowerId(jsonObject1.getInt("powerId"));
                bean.setPowerName(jsonObject1.getString("powerName"));
                bean.setEnds(1);
                beanList.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d(TAG, beanList.toString());
        return beanList;
    }


//    private List<PowerModel> requestData(){
//        List<PowerModel> result = new ArrayList<>();
//        List<DataBean> beanList = getData();
//
//        for(int i=0;i<beanList.size();i++){
//            PowerModel powerModel = new PowerModel();
//            DataBean bean = beanList.get(i);
//            powerModel.setName(bean.getPowerName());
//            powerModel.setParentId(bean.getParentId() + "");
//            powerModel.setId(bean.getPowerId() + "");
//            powerModel.setChild(new ArrayList<PowerModel>());
//            result.add(powerModel);
//        }
////        List<PowerModel> realResult = new ArrayList<>();
//        for(int i=0;i<result.size();i++){
//            PowerModel p1 = result.get(i);
//            for(int j=1+i,len = result.size();j<len;j++){
//                PowerModel p2 = result.get(j);
//                if(p1.getId().equals(p2.getParentId())){
//                    p1.getChild().add(p2);
//
//                    result.remove(p2);
//                    len--;
//                    j--;
//                }else if(p1.getParentId().equals(p2.getId())){
//                    p2.getChild().add(p1);
//
//                    result.remove(p1);
//                    len--;
//                    j--;
//                }
//            }
//        }
////        removeDuplicate(realResult);
//        return result;
//    }

//    public   static   void  removeDuplicate(List list)   {
//        HashSet h  =   new  HashSet(list);
//        list.clear();
//        list.addAll(h);
//        System.out.println(list);
//    }


//    private List<DataBean> getChilds(int id){
//        List<DataBean> childs = new ArrayList<>();
//        for(DataBean bean:beanList){
//            if(bean.getParentId() == id){
//                childs.add(bean);
//            }
//        }
//        return childs;
//    }

//    private void travel(DataBean bean){
//        List<DataBean> childs = getChilds(bean.getPowerId());
//        if(childs!=null){
//            for(DataBean child:childs){
//                bean.setPowerName(child.getPowerName());
//                bean.setPowerId(child.getPowerId());
//                bean.setParentId(child.getParentId());
//                travel(child);
//            }
//        }
//    }

//    private void request(){
//        for(int i=0;i<beanList.size();i++){
//            travel(beanList.get(i));
//        }
//        Log.d(TAG, beanList.toString());
//    }

//    private List<PowerModel> requestData(){
//        List<PowerModel> result = new ArrayList<>();
//        List<DataBean> beanList = getData();
//
//        for(int i=0;i<beanList.size();i++){
//            PowerModel powerModel = new PowerModel();
//
//            for(int j=i+1;j<beanList.size();j++){
//
//            }
//
//            DataBean bean = beanList.get(i);
//            powerModel.setName(bean.getPowerName());
//            powerModel.setParentId(bean.getParentId() + "");
//            powerModel.setId(bean.getPowerId() + "");
//            powerModel.setChild(new ArrayList<PowerModel>());
//            result.add(powerModel);
//        }
//
//        for(int i=0;i<result.size();i++){
//            PowerModel p1 = result.get(i);
//            for(int j=1+i;j<result.size();j++){
//                PowerModel p2 = result.get(j);
//                if(p1.getId().equals(p2.getParentId())){
//                    p1.getChild().add(p2);
//                }else if(p1.getParentId().equals(p2.getId())){
//                    p2.getChild().add(p1);
//                }
//            }
//        }
//
//
//        return result;
//    }


}
